/* AndyDroid - Android automation testing Framework.
 Copyright (C) 2011 MicroStrategy Test Automation Team

 AndyDroid is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with software. If not, contact us.
 
 @email:	robort1002@gmail.com
 */
package com.microstrategy.android.automation.tester;

import android.util.Log;

public class WebElementTester {
 private final WebViewTester mAndyWebView;
	
	public WebElementTester(WebViewTester AndyWebView){
		mAndyWebView = AndyWebView;
	}
	
	public WebElementTester nextSibling() {
		//TODO
		return this;
	}
	
	public String innerHTML(){
		return executeJavaScriptAndReturn("element.innerHTML");	
	}
	
	public void click() {
		Log.d("AndyActivity","click in webelement");
		loadJavascript("element.dispatchEvent(e)");
	}
	
	
	private String executeJavaScriptAndReturn(String script) {
		loadJavascript("window.webdriver.executejs("+script+")");
		return JavascriptInterface.getInstance().getResult();
	}
	
	public void executeJs(final String script) throws InterruptedException {
		loadJavascript(script);
	}
	
	private void loadJavascript(String script) {
//		this.currentScript = script;
		mAndyWebView.getInterface().setReady(false);
		mAndyWebView.getWebView().loadUrl("javascript:"+ script);
	}
}
